package com.hrms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrms.entity.Stay;
import com.hrms.mapper.StayMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

@Service
public class StayServiceImpl extends ServiceImpl<StayMapper, Stay> implements StayService {
    @Autowired
    private StayMapper stayMapper;

    public List<Map<String, Object>> selectAll() {
        // 返回表stay的所有记录
        return listMaps();
    }

    public boolean add(Stay stay) {
        // 在表stay中添加一条记录
        // true表示添加成功
        return save(stay);
    }

    public Integer selectUser(Stay stay) {
        // 根据stay_id返回相应的user_id
        QueryWrapper<Stay> wrapper = new QueryWrapper<>();
        wrapper.eq("stay_id", stay.getStay_id())
                .select("user_id");

        List<Map<String, Object>> list = listMaps(wrapper);

        return (Integer) list.get(0).get("user_id");
    }

    public String selectEmployee(Stay stay) {
        // 根据stay_id返回相应的employee_id
        QueryWrapper<Stay> wrapper = new QueryWrapper<>();
        wrapper.eq("stay_id", stay.getStay_id())
                .select("employee_id");

        List<Map<String, Object>> list = listMaps(wrapper);

        return list.get(0).get("employee_id").toString();
    }

    public String selectRoom(Stay stay) {
        // 根据stay_id返回相应的room_id
        QueryWrapper<Stay> wrapper = new QueryWrapper<>();
        wrapper.eq("stay_id", stay.getStay_id())
                .eq("stay_multiple_id", stay.getStay_multiple_id())
                .select("room_id");

        List<Map<String, Object>> list = listMaps(wrapper);

        return list.get(0).get("room_id").toString();
    }

    public boolean updateEndTime(Stay stay) {
        // 根据stay_id修改stay_end_time
        // true表示修改成功
        QueryWrapper<Stay> wrapper = new QueryWrapper<>();
        wrapper.eq("stay_id", stay.getStay_id());

        return update(stay, wrapper);
    }

    public boolean updateEmployeeId(Stay stay) {
        // 根据stay_id修改employee_id
        // true表示修改成功
        QueryWrapper<Stay> wrapper = new QueryWrapper<>();
        wrapper.eq("stay_id", stay.getStay_id())
                .select("employee_id");

        return update(stay, wrapper);
    }

    public boolean delete(Stay stay) {
        // 根据stay_id在表stay中删除一条记录
        // true表示删除成功
        QueryWrapper<Stay> wrapper = new QueryWrapper<>();
        wrapper.eq("stay_id", stay.getStay_id())
                .eq("stay_multiple_id", stay.getStay_multiple_id());
        return remove(wrapper);
    }

    public List<Map<String, Object>> selectbyUser(Stay stay) {
        // 表stay连接表room
        // 根据user_id返回除employee_id的相应记录
        return stayMapper.selectbyUser(stay.getUser_id());
    }


    public List<Map<String, Object>> selectbyEmployeeInit() {
        // 表stay连接表room的所有记录
        // room_status不为“已删除”
        return stayMapper.selectbyEmployeeInit();
    }

    public List<Map<String, Object>> checkoutProcessInit() {
        // 表stay连接表room的所有记录
        // room_status为“退房中”
        return stayMapper.checkoutProcessInit();
    }

    public List<Map<String, Object>> selectAllTable() {
        // 表stay连接表user、表employee、表room
        // 其中表user连接表customer
        // 返回五张表的所有记录
        return stayMapper.selectAllTable();
    }

    public String selectStartTime(Stay stay) {
        // 根据stay_id返回相应的stay_start_time
        QueryWrapper<Stay> wrapper = new QueryWrapper<>();
        wrapper.eq("stay_id", stay.getStay_id())
                .select("stay_start_time");

        List<Map<String, Object>> list = listMaps(wrapper);

        return list.get(0).get("stay_start_time").toString();
    }

    public String selectEndTime(Stay stay) {
        // 根据stay_id返回相应的stay_end_time
        QueryWrapper<Stay> wrapper = new QueryWrapper<>();
        wrapper.eq("stay_id", stay.getStay_id())
                .select("stay_end_time");

        List<Map<String, Object>> list = listMaps(wrapper);

        return list.get(0).get("stay_end_time").toString();
    }

    public String selectStayName(Stay stay) {
        // 根据stay_id返回相应的stay_name
        QueryWrapper<Stay> wrapper = new QueryWrapper<>();
        wrapper.eq("stay_id", stay.getStay_id())
                .eq("stay_multiple_id", stay.getStay_multiple_id())
                .select("stay_name");

        List<Map<String, Object>> list = listMaps(wrapper);

        return list.get(0).get("stay_name").toString();
    }

    public Integer selectStayMultipleID(Stay stay) {
        // 根据stay_id返回最后一个stay_multiple_id
        QueryWrapper<Stay> wrapper = new QueryWrapper<>();
        wrapper.eq("stay_id", stay.getStay_id())
                .select("stay_multiple_id");

        List<Map<String, Object>> list = listMaps(wrapper);

        String stay_multiple_id = list.get(list.size() - 1).get("stay_multiple_id").toString();
        return Integer.parseInt(stay_multiple_id);
    }
}
